package br.com.news.web.service;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.core.MediaType;

import br.com.news.utils.database.beans.EnqueteBean;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;

public class EnqueteService {

	public void save(EnqueteBean enquete) {	
		Gson g = new Gson();
		String json = g.toJson(enquete);

		Client c = Client.create();
	    c.resource("http://localhost:8080/news-web-service-rest/enquetes/insert").type(MediaType.APPLICATION_JSON+ ";charset=utf-8").post(json);	 
	}
	
	public void update(EnqueteBean enquete) {		 
		Gson g = new Gson();
		String json = g.toJson(enquete);

		Client c = Client.create();
	    c.resource("http://localhost:8080/news-web-service-rest/enquetes/update").type(MediaType.APPLICATION_JSON+ ";charset=utf-8").put(json);	
	}
	
	@SuppressWarnings("null")
	public void votar(String voto, EnqueteBean enquete) {
		Client c = Client.create();		
		Long idEnquete = enquete.getIdEnquete();
		
		if(voto != null || !voto.isEmpty()) {
			if(voto.equals("1")) {
				c.resource("http://localhost:8080/news-web-service-rest/resposta/enquete/web/insert/"+idEnquete+"/1").post();
			} else {
				c.resource("http://localhost:8080/news-web-service-rest/resposta/enquete/web/insert/"+idEnquete+"/2").post();
			}
		}
	}
	
	public List<EnqueteBean> findByQuestion(EnqueteBean enquete) {		 
		Gson g = new Gson();
        String json = g.toJson(enquete);

	    Client c = Client.create();
	    String respostaJson = c.resource("http://localhost:8080/news-web-service-rest/enquetes/findByQuestion").accept(MediaType.APPLICATION_JSON+ ";charset=utf-8").type(MediaType.APPLICATION_JSON+ ";charset=utf-8").post(String.class,json);
	    Gson gson = new Gson();
	    List<EnqueteBean> enquetes = gson.fromJson(respostaJson, new TypeToken<ArrayList<EnqueteBean>>(){}.getType());
	    
	    return enquetes;	   
	}
	
	public List<EnqueteBean> findPollByStatus(boolean status) {
	    Client c = Client.create();
	    WebResource wr = c.resource("http://localhost:8080/news-web-service-rest/enquetes/findPollByStatus/"+status);
	    String json = wr.get(String.class);
	    Gson gson = new Gson();
	    List<EnqueteBean> enquetes = gson.fromJson(json, new TypeToken<ArrayList<EnqueteBean>>(){}.getType());
	    
	    return enquetes;
	}
	
	public List<EnqueteBean> pollActive() {
	    Client c = Client.create();
	    WebResource wr = c.resource("http://localhost:8080/news-web-service-rest/enquetes/findPollActive");
	    String json = wr.get(String.class);
	    Gson gson = new Gson();
	    List<EnqueteBean> enquetes = gson.fromJson(json, new TypeToken<ArrayList<EnqueteBean>>(){}.getType());
	    
		return enquetes;
	}
	
	public List<EnqueteBean> lastPoll() {
	    Client c = Client.create();
	    WebResource wr = c.resource("http://localhost:8080/news-web-service-rest/enquetes/last/poll");
	    String json = wr.get(String.class);
	    Gson gson = new Gson();
	    List<EnqueteBean> enquetes = gson.fromJson(json, new TypeToken<ArrayList<EnqueteBean>>(){}.getType());
	    
		return enquetes;
	}
		
}
